package org.LZQ.ShortLink.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.LZQ.ShortLink.admin.common.convention.result.Result;
import org.LZQ.ShortLink.admin.common.convention.result.Results;
import org.LZQ.ShortLink.admin.remote.RemoteService;
import org.LZQ.ShortLink.admin.remote.ShortLinkActualRemoteService;
import org.LZQ.ShortLink.admin.remote.dto.req.ShortLinkBatchCreateReqDTO;
import org.LZQ.ShortLink.admin.remote.dto.req.ShortLinkCreateReqDTO;
import org.LZQ.ShortLink.admin.remote.dto.req.ShortLinkPageReqDTO;
import org.LZQ.ShortLink.admin.remote.dto.req.ShortLinkUpdateReqDTO;
import org.LZQ.ShortLink.admin.remote.dto.resp.ShortLinkBaseInfoRespDTO;
import org.LZQ.ShortLink.admin.remote.dto.resp.ShortLinkBatchCreateRespDTO;
import org.LZQ.ShortLink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import org.LZQ.ShortLink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.LZQ.ShortLink.admin.util.EasyExcelWebUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortController {
    private final RemoteService remoteService;

    private final ShortLinkActualRemoteService shortLinkActualRemoteService = new ShortLinkActualRemoteService(){
    };
    @PostMapping("/api/shortLink/admin/shortLink/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam) {
            return remoteService.createShortLink(requestParam);
        }
    /**
     * 批量创建短链接
     */
    @SneakyThrows
    @PostMapping("/api/shortLink/admin/shortLink/create/batch")
    public void batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam, HttpServletResponse response) {
        Result<ShortLinkBatchCreateRespDTO> shortLinkBatchCreateRespDTOResult = remoteService.batchCreateShortLink(requestParam);
        if (shortLinkBatchCreateRespDTOResult.isSuccess()) {
            List<ShortLinkBaseInfoRespDTO> baseInfoRespDTOS = shortLinkBatchCreateRespDTOResult.getData().getBaseLinkInfos();
            EasyExcelWebUtil.write(response, "批量创建短链接", ShortLinkBaseInfoRespDTO.class,baseInfoRespDTOS);
        }
    }

    @PostMapping("/api/shortLink/admin/shortLink/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam) {
        remoteService.updateShortLink(requestParam);
        return Results.success();
    }

    @GetMapping("/api/shortLink/admin/shortLink/page")
    public Result<Page<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam) {
            return remoteService.pageShortLink(requestParam.getGid(), requestParam.getOrderTag(), requestParam.getCurrent(), requestParam.getSize());
        }





}
